﻿using Microsoft.AspNetCore.SignalR;

namespace WebApi.Hubs;

/// <summary>
/// SignalR Hub
/// </summary>
public class StronglyTypedChatHub : Hub<IChatClient>
{
    /// <summary>
    /// Send message to all clients
    /// </summary>
    /// <param name="message"></param>
    /// <param name="userName"></param>
    /// <returns></returns>
    /*public async Task SendMessage(string userName, string message)
    {
        await Clients.All.ReceiveMessage(userName, message);
    }*/
    public async Task SendMessage(ChatMessage  chatMessage)
    {
        await Clients.All.ReceiveMessage(chatMessage);
    }

    /// <summary>
    /// Send message to caller
    /// </summary>
    /// <param name="user"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    public async Task SendMessageToCaller(string user, string message)
    {
        await Clients.Caller.ReceiveMessage(user, message);
    }

    /// <summary>
    /// Send message to group
    /// </summary>
    /// <param name="groupName"></param>
    /// <param name="user"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    public async Task SendMessageToGroup(string groupName, string user, string message)
    {
        await Clients.Group(groupName).ReceiveMessage(user, message);
    }


    /* /// <summary>
     /// 服务端等待客户端消息
     /// </summary>
     /// <param name="connectionId"></param>
     /// <param name="token"></param>
     /// <returns></returns>
     public async Task<string> WaitForMessage(string connectionId, CancellationToken token)
     {
         var message = await Clients.Client(connectionId).GetMessage(connectionId, token);
         return message;
     }*/

    /// <summary>
    /// 给特定用户发送消息
    /// </summary>
    /// <param name="userId"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    public async Task SendMessageToUser(string userId, string message)
    {
        await Clients.User(userId).ReceiveMessage(userId, message);
    }

    /// <summary>
    /// 用户加入组
    /// </summary>
    /// <param name="groupName"></param>
    /// <returns></returns>
    public async Task AddToGroup(string groupName)
    {
        await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
        await Clients.Group(groupName).Send($"{Context.ConnectionId} has joined the group {groupName}");
    }

    /// <summary>
    /// 从组中移除用户
    /// </summary>
    /// <param name="groupName"></param>
    /// <returns></returns>
    public async Task RemoveFromGroup(string groupName)
    {
        await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
        await Clients.Group(groupName).Send( $"{Context.ConnectionId}离开了{groupName}组");
    }

    /// <summary>
    /// 使用自定义对象作为参数，能够向后兼容
    /// </summary>
    /// <param name="netClient"></param>
    /// <returns></returns>
    public async Task NetClientSendMessage(NetClientRequestParam netClient)
    {
        // 发送消息到所有连接
        await Clients.All.NetClientReceiveMessage(netClient);
    }


}

/// <summary>
/// 客户端传递过来的参数
/// </summary>
public class ChatMessage
{
    /// <summary>
    /// 用户名
    /// </summary>
    public string? UserName { get; set; }

    /// <summary>
    /// 消息
    /// </summary>
    public string? Message { get; set; }
}